package cn.harry.modular.cp.service.impl;

import cn.harry.modular.cp.domain.EvaluationContent;
import cn.harry.modular.cp.domain.EvaluationUser;
import cn.harry.modular.cp.mapper.EvaluationUserMapper;
import cn.harry.modular.cp.service.EvaluationContentService;
import cn.harry.modular.cp.vo.StatisticalResults;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.harry.modular.cp.mapper.EvaluationMapper;
import cn.harry.modular.cp.service.EvaluationService;
import cn.harry.modular.cp.domain.Evaluation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 评测服务实现类
 *
 * @author harry
 * @公众号 Harry技术
 */
@Service
@RequiredArgsConstructor
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {
    private final EvaluationContentService evaluationContentService;
    private final EvaluationUserMapper evaluationUserMapper;

    @Override
    public List<StatisticalResults> statisticalResults(Long id) {
        Evaluation evaluation = this.getById(id);
        Assert.notNull(evaluation, "评测不存在");
        List<EvaluationContent> evaluationContents = evaluationContentService.list(Wrappers.<EvaluationContent>lambdaQuery().eq(EvaluationContent::getEvaluationId, id));
        if (evaluationContents.isEmpty()) {
            return List.of();
        }

        // 登录码=被测评人信息 + 标识(公司领导/基层领导/员工代表) + (公司领导/基层领导/员工代表)评测人数限制
        // 根据 evaluationUserId 分组 求平均分数
        Map<Long, List<EvaluationContent>> evaluationContentMap = evaluationContents.stream().collect(Collectors.groupingBy(EvaluationContent::getEvaluationUserId));
        List<StatisticalResults> statisticalResults = new ArrayList<>();
        for (Map.Entry<Long, List<EvaluationContent>> entry : evaluationContentMap.entrySet()) {
            StatisticalResults item = new StatisticalResults();
            item.setEvaluationId(id);
            item.setEvaluationUserId(entry.getKey());
            cover(item, entry.getValue());
            item.setLimitBasicLeaders(evaluation.getLimitBasicLeaders());
            item.setLimitCompanyLeaders(evaluation.getLimitCompanyLeaders());
            item.setLimitEmployee(evaluation.getLimitEmployee());
            statisticalResults.add(item);
        }
        return statisticalResults;
    }

    private void cover(StatisticalResults statisticalResult, List<EvaluationContent> list) {
        Long evaluationUserId = statisticalResult.getEvaluationUserId();
        // 平均分
        double averageScore = list.stream().mapToDouble(EvaluationContent::getScoreSum).average().orElse(0);
        statisticalResult.setFinalScore(averageScore);
        // 评测人数
        EvaluationUser user = evaluationUserMapper.selectById(evaluationUserId);
        statisticalResult.setName(user.getName());
        statisticalResult.setLoginCode(user.getLoginCode());
    }
}

